/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vadung.contragame2d;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author dung.voanh
 */
public class StateConfirmMenu extends State {
    // Index of submenu
    static final int SELECT_YES = 0;
    static final int SELECT_NO = 1; 
    
    private Font m_fontTitle;
    private Font m_fontSubMenu;
    private String m_astrSubMenu[];
    private int m_iSelectedIndex = 0;
    private boolean m_haveSelected;
    private boolean m_haveTouchPrevious = false; 

    public int GetSelectedIndex() {
        return m_iSelectedIndex;
    }

    public boolean HaveSelected() {
        return m_haveSelected;
    }

    public StateConfirmMenu() {
    }

    private void NextSelect() {
        if (m_iSelectedIndex < m_astrSubMenu.length - 1) {
            m_iSelectedIndex++;
        } else {
            m_iSelectedIndex = 0;
        }
    }

    private void PreviousSelect() {
        if (m_iSelectedIndex > 0) {
            m_iSelectedIndex--;
        } else {
            m_iSelectedIndex = m_astrSubMenu.length - 1;
        }
    }

    public void Init() {
        m_haveSelected = false;
        m_fontTitle = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
        m_fontSubMenu = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);

        m_astrSubMenu = new String[]{GLOBAL.s_strMenu_Confirm_Yes, GLOBAL.s_strMenu_Confirm_No};
        SoundManger.GetInstance().PlaySound(SoundManger.s_iSound_boss);
    }

    public void Update() {
        if (HaveSelected()) {
            //SoundManger.GetInstance().PlaySound(SoundManger.s_iSound_fanfare);
            switch (GetSelectedIndex()) {
                case StateConfirmMenu.SELECT_NO: {
                    SoundManger.GetInstance().StopPlayAll();
                    Midlet.s_canGame.SwitchState(DEFINE.STATE_MAINMENU);
                }
                break;

                case StateConfirmMenu.SELECT_YES: {
                    System.out.println("Menu confirm- selected Exit game.");
                    Midlet.s_canGame.Exit();
                }
                break;
            }
            m_haveSelected = false;
        }
        int key = Midlet.s_canGame.GetKeyPressAction();
        if (key != 0) {
            System.out.println("Menu confirm Key press: " + key);
            switch (key) {
                case DEFINE.KEY_UP:
                    PreviousSelect();
                    break;

                case DEFINE.KEY_DOWN:
                    NextSelect();
                    break;

                case DEFINE.KEY_FIRE:
                    m_haveSelected = true;
                    break;
                default: {
                    m_haveSelected = false;
                }
            }
        }
        try {
            Thread.sleep(70);
        } catch (InterruptedException ex) {
        }

        int height = Midlet.s_canGame.getHeight();
        int width = Midlet.s_canGame.getWidth();
        int x = GLOBAL.TOUCH_X;
        int y = GLOBAL.TOUCH_Y;

        if (GLOBAL.IS_TOUCH) {
            m_haveTouchPrevious = true;

            int ySubMenu;
            for (int i = 0; i < m_astrSubMenu.length; i++) {
                ySubMenu = 35 + i * 25;
                if (y >= ySubMenu && y < ySubMenu + 25) {
                    m_iSelectedIndex = i;
                    break;
                }
            }
        } else if (m_haveTouchPrevious && GLOBAL.IS_TOUCH == false) {            
            int ySubMenu = 35 + m_iSelectedIndex * 25;
            if (y >= ySubMenu && y < ySubMenu + 25) {
                m_haveSelected = true;
            }
            m_haveTouchPrevious = false;
        }
    }

    public void Render(Graphics g) {
        int height = Midlet.s_canGame.getHeight();
        int width = Midlet.s_canGame.getWidth();
        // Select black color
        g.setColor(0, 0, 0);
        g.fillRect(0, 0, width, height);

        // Draw menu title
        g.setColor(255, 0, 0); // Select red color
        g.setFont(m_fontTitle);
        g.drawString(GLOBAL.s_strMenuTitle_Exit, width / 2, 5, Graphics.TOP | Graphics.HCENTER);

        //Draw sub menu
        g.setFont(m_fontSubMenu);
        for (int i = 0; i < m_astrSubMenu.length; i++) {
            if (i == m_iSelectedIndex) {
                g.setColor(130, 130, 130); // Select gray color
                g.fillRect(0, 35 + i * 25, width, 25);
                g.setColor(0, 255, 0); // Select green color
            } else {
                g.setColor(255, 255, 255); // Select while color
            }
            g.drawString(m_astrSubMenu[i], width / 2, 40 + i * 25, Graphics.TOP | Graphics.HCENTER);
        }
    }

    public void Exit() {
        m_fontSubMenu = null;
        m_fontTitle = null;
    }
//    public void HandleMessage(int message) {
//    }
}
